package com.fx.single.repo.impl;

import com.fx.single.repo.BaseQueryDsl;
import com.fx.single.model.AjqkDTO;
import com.fx.single.model.JjqkDTO;
import com.fx.single.model.ReportEnum;
import com.fx.single.model.dsl.QYwbJqJjjlb;
import com.fx.single.repo.SingleRepo;
import com.querydsl.jpa.impl.JPAQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @desc
 * @author dadovicn
 * @date   2019/1/4
 */
@Slf4j
@Component
public class SingleRepoImpl extends BaseQueryDsl implements SingleRepo {

    private static final int TIME_CONTROL = 0 + 150;
    /**
     * 早上六点开始的/
     * @return
     */
    @Override
    public JjqkDTO getJjqk(int threshold) {
        LocalDateTime startTime = getStart(threshold);
        JjqkDTO jjqkDTO = new JjqkDTO();
        QYwbJqJjjlb jjb = QYwbJqJjjlb.ywbJqJjjlb;
        Long jjTotal =  queryFactory.select(jjb.jjbh).from(jjb).where(jjb.bjsj.gt(startTime)).fetchAll().fetchCount();
        jjqkDTO.setJjTotal(jjTotal);
        Long validTotal = queryFactory.select(jjb.jjbh).from(jjb).where(jjb.bjsj.gt(startTime).and(jjb.cjbs.eq("1"))).fetchAll().fetchCount();
        jjqkDTO.setValidTotal(validTotal);
        return jjqkDTO;
    }

    @Override
    public AjqkDTO getAjqk(int threshold) {
        LocalDateTime startTime = getStart(threshold);
        QYwbJqJjjlb jj = QYwbJqJjjlb.ywbJqJjjlb;
        Long XSAJ = queryFactory.select(jj.jjbh).from(jj).where(jj.dxbjlb.eq(ReportEnum.XSAJ.getValue()).and(jj.bjsj.gt(startTime))).fetchAll().fetchCount();
        Long ZHIAN = queryFactory.select(jj.jjbh).from(jj).where(jj.dxbjlb.eq(ReportEnum.ZHIAN.getValue()).and(jj.bjsj.gt(startTime))).fetchAll().fetchCount();
        Long QUNZHONG = queryFactory.select(jj.jjbh).from(jj).where(jj.dxbjlb.eq(ReportEnum.QUNZHONG.getValue()).and(jj.bjsj.gt(startTime))).fetchAll().fetchCount();
        Long JIAOTONG = queryFactory.select(jj.jjbh).from(jj).where(jj.dxbjlb.eq(ReportEnum.JIAOTONG.getValue()).and(jj.bjsj.gt(startTime))).fetchAll().fetchCount();
        Long JUBAO = queryFactory.select(jj.jjbh).from(jj).where(jj.dxbjlb.eq(ReportEnum.JUBAOTOUSU.getValue()).and(jj.bjsj.gt(startTime))).fetchAll().fetchCount();
        AjqkDTO ajqkDTO = new AjqkDTO(XSAJ, ZHIAN, QUNZHONG, JIAOTONG, JUBAO);
        return ajqkDTO;
    }

    public LocalDateTime getStartTime(int minusDays, int threshold) {
        return LocalDateTime.of(LocalDate.now().minusDays(minusDays), LocalTime.MIN).plusHours(threshold);
    }

    public LocalDateTime getStart(int threshold) {
        int now = LocalDateTime.now().minusDays(TIME_CONTROL).getHour();
        // 正常情况下计算当前周期
        LocalDateTime startTime = getStartTime(TIME_CONTROL, threshold);
        if (now < threshold) {
            // 非正常情况下上一个周期
            startTime = getStartTime(TIME_CONTROL + 1, threshold);
        }
        return startTime;
    }
}








